/**
 * OrganizationDaoImpl.java
 * Created at 2017-07-06
 * Created by Jackie liu
 * Copyright (C) 2014, All rights reserved.
 */
package com.huabo.dao.impl.oracle;

import com.huabo.base.BaseDaoImpl;
import com.huabo.dao.inter.OrganizationDao;
import com.huabo.model.Area;
import com.huabo.model.Organization;
import com.huabo.sql.builder.SelectBuilder;
import com.huabo.sql.builder.WhereBuilder;
import com.huabo.utils.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * <p>ClassName: OrganizationDaoImpl</p>
 * <p>Description: 组织机构Dao实现</p>
 * <p>Author: Jackie liu</p>
 * <p>Date: 2017-07-06</p>
 */
@Repository
public class OrganizationDaoImpl extends BaseDaoImpl<Organization, Long> implements OrganizationDao {

    @Override
    public boolean isDuplicateCode(Long id, String code) {
        return isDuplicateField(id, code, "F_CODE");
    }

    @Override
    public boolean isDuplicateName(Long id, String name) {
        return isDuplicateField(id, name, "F_NAME");
    }

    @Override
    public Page<Organization> queryPage(Map<String, Object> params) {
        WhereBuilder queryCondition = new WhereBuilder();
        //queryCondition.setOrderBy("A.F_ORDERNUM ASC ");
        if (params != null) {
            initPageParam(queryCondition, params);
            queryCondition.andFullLike("A.F_ID_PATH", params.get("idPath"));
            String searchInput = (String) params.get("searchInput");
            if (StringUtils.isNotEmpty(searchInput)) {
                queryCondition.andGroup()
                        .andFullLike("A.F_NAME", searchInput)
                        .orFullLike("A.F_CODE", searchInput);
            }
        }
        //String sql = "SELECT A.*,B.F_NAME AS F_AREA_NAME FROM TS_ORGANIZATION A LEFT JOIN TC_AREA B ON A.F_AREA_ID=B.F_ID";

        SelectBuilder selectBuilder = selectBuilder("A");
        selectBuilder.column("B.F_NAME AS F_AREA_NAME")
                .from(domainModelAnalysis.getTableName()).alias("A").build()
                .leftJoin(Area.class).alias("B").on("A.F_AREA_ID=B.F_ID").build();

        return queryPage(selectBuilder.getSql(), queryCondition);
    }

    @Override
    public List<Organization> queryList(Map<String, Object> params) {
        WhereBuilder queryCondition = new WhereBuilder();
        //queryCondition.setOrderBy("_ORDERNUM ASC ");
        if (params != null) {
            queryCondition.andFullLike("F_ID_PATH", params.get("idPath"));
            queryCondition.andFullLike("F_NAME", params.get("name"));
            queryCondition.andEquals("F_CODE", params.get("code"));
            queryCondition.andIn("F_ID", (String[]) params.get("ids"));
        }
        return query(queryCondition);
    }

    @Override
    public Organization queryById(Long id) {
        //String sql = "SELECT A.*,B.F_NAME AS F_AREA_NAME FROM TS_ORGANIZATION A LEFT JOIN TC_AREA B ON A.F_AREA_ID=B.F_ID WHERE A.F_ID = ?";
        WhereBuilder wb = new WhereBuilder();
        wb.andEquals("A.F_ID", id);

        SelectBuilder selectBuilder = selectBuilder("A");
        selectBuilder.column("B.F_NAME AS F_AREA_NAME")
                .from(domainModelAnalysis.getTableName()).alias("A").build()
                .leftJoin(Area.class).alias("B").on("A.F_AREA_ID=B.F_ID").build();

        List<Organization> list = query(selectBuilder.getSql(), wb);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<Organization> queryByCategory() {
        WhereBuilder queryCondition = new WhereBuilder();
        queryCondition.andEquals("F_CATEGORY", Organization.Category.SHI_JU.getValue());
        return query(queryCondition);
    }

    @Override
    public List<Organization> queryChildrenOrg(Long id) {
        WhereBuilder queryCondition = new WhereBuilder();
        queryCondition.andEquals("F_CATEGORY", Organization.Category.XIAN_JU.getValue());
        queryCondition.andFullLike("F_ID_PATH", id);
        return query(queryCondition);
    }

    //根据areaId查找对应的单位组织
    @Override
    public Organization queryByAreaId(Long areaId) {
        WhereBuilder queryCondition = new WhereBuilder();
        queryCondition.andEquals("F_AREA_ID", areaId);
        List<Organization> list = query(queryCondition);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Organization> queryChildOrgList(Long parentId) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderBy("F_CODE");
        wb.andEquals("F_PARENT_ID", parentId);
        return query(wb);
    }
}